<script>

export default {
  name: 'UserinfoDetail',
  data () {
    return {
      show: false,
      dataForm: {},
      rules: {
        nickname: [{ required: true, message: '昵称不能为空' }],
        phone: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1\d{10}$/, message: '手机号码格式错误' }
        ]
      },
      roleList: []
    }
  },
  methods: {
    getRoleList () {
      this.$axios.get('role', { params: { param: 'select' } }).then(data => {
        this.roleList = [...data]
      }).catch(e => e)
    },
    showDialog () {
      this.getRoleList()
      this.show = true
      /* 添加和修改都对表单数据进行一次清空，这样可以去掉修改后再点添加按钮的缓存数据 */
      /* 在dialog组件上使用v-if对其进行判断，可以去掉修改后再点添加按钮的验证缓存 */
      this.dataForm = { ...this.$options.data().dataForm }
      this.$axios.get('userinfo',
        {
          params: {
            param: 'info'
          }
        }
      ).then(data => {
        this.dataForm = { ...data }
      }).catch(e => e)
    },
    submit () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$axios.post('userinfo?param=update', {
            id: this.dataForm.id,
            nickname: this.dataForm.nickname,
            phone: this.dataForm.phone
          }).then(() => {
            this.$message.success('更新成功！')
            this.show = false
            this.$emit('close')
          }).catch(e => e)
        }
      })
    }
  }
}
</script>

<template>
  <el-dialog title="用户详情" :visible.sync="show" v-if="show">
    <el-form :model="dataForm" :rules="rules" label-width="80px" ref="dataForm">
      <el-form-item label="用户名">
        <el-input disabled v-model="dataForm.username"/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="请输入昵称" clearable/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="请输入手机号" clearable/>
      </el-form-item>
      <el-form-item label="角色">
        <el-select style="width: 100%" v-model="dataForm.roleId" disabled>
          <el-option v-for="role in roleList" :key="role.id" :value="role.id" :label="role.roleName" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="dataForm.status" disabled>
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
